<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Tools extends MY_Controller {
    
    public function __construct() {
        parent::__construct();
        ini_set('zlib.output_compression', 'On'); 
        ini_set('zlib.output_compression_level', 6); 
        ini_set('php_flag zlib.output_compression', 'On'); 
        if (isset($_GET['phpinfo'])) {
            echo phpinfo();
            exit;
        }
        
        if (! isset($_GET['key']) || (isset($_GET['key']) && $_GET['key'] != '223088')) {
            exit('No authorise');
        }
        
        $this->load->model('Karas_model');
        $this->load->model('Places_model');
    }
    
    public function insert() {
        $handle = fopen("public/tools/kara.txt", 'r');
        
        if ($handle) {
            $total_insert = 0;
            $total_existed = 0;
            while ($line = fgets($handle)) {
                $data = explode('+', $line);
                $check = $this->Karas_model->get_where(array(
                    'code'  => $data[0]
                ));
               
                if ($result = $check->result_array()) {
                    echo "Existed song code = {$result[0]['code']}, name = {$result[0]['name']} </br>";
                    $total_existed++;
                    
//                    $this->Karas_model->update_by_code(array(
//                    		'vol'		=>  51
//                    ), $data[0]);
                    
                    continue;
                }
                
                if (isset($data[0]) && isset($data[1]) && isset($data[2]) && isset($data[3])) {
                    $this->Karas_model->insert(array(
                        'code'          =>  $data[0],
                        'name'          =>  $data[1],
                        'short_cont'    => str_replace('…', '',$data[2]),
                        'author'        =>  $data[3],
                    	'vol'			=>  51
                    ));
                    
                    echo "Insert song code = $data[0], name = $data[1] </br>";
                    $total_insert++;
                }
            }
            
            echo "Total songs insert = $total_insert<br/>";
            echo "Total songs existed = $total_existed";
        }
        fclose($handle);
        exit;
    }
    
    public function filter_vn() {
    	$data = $this->Karas_model->get_all()->result_array();
    	foreach ($data as $item) {
    		$this->Karas_model->update(array('short_cont1' => $this->vn_str_filter($item['short_cont'])), $item['id']);
    	}
    }
    
    public function addlocation() {
        $handle = fopen('public/tools/location.txt', 'r');
        $total_existed = 0;
        if ($handle) {
            while ($line = fgets($handle)) {
                $data = explode('+++', $line);
                
                $check = $this->Places_model->get_where(array(
                    'name'  => $data[1]
                ));
               
                if ($result = $check->result_array()) {
                    echo "Existed places name = {$result[0]['name']} </br>";
                    $total_existed++;
                    continue;
                }
                
                $address = explode(',', $data[3]);
                $img_content_file = file_get_contents($data[0]);
                $filter_name = $this->vn_str_filter($data[1]);
                $img_name = str_replace('  ', ' ', $filter_name);
                $img_name = 'efind-ho-chi-minh-karaoke-'. str_replace(' ', '-', $img_name). '.jpg';
                file_put_contents("public/static/karaoke/{$img_name}", $img_content_file);
                
                // Get latitude and longitude
                $lat_lng = $this->get_lnglat($data[3]);
                
                $this->Places_model->insert(array(
                        'name'          => $data[1],     
                        'img'           => $img_name,
                        'short_cont'    => '',
                        'address'       => $data[3],
                        'dictrict'      => ! empty($address[1]) ? $address[1] : '',
                        'province'      => 'Hà Nội',
                        'provinceid'    => 'ha-noi',
                        'latitude'      => $lat_lng['latitude'],
                        'longitude'     => $lat_lng['longitude'],
                        'phone'         => $data[2]
                    )
                );
            }
        }
        exit;
    }
    
    function create_xml_file() {
        $xml = new DOMDocument();
        
        // Get all the songs
        $this->load->model('karas_model');
        $songs = $this->karas_model->get_all(null, null, 'name')->result_array();
        
        // Create root node
        $xml_album = $xml->createElement('songs');
        foreach ($songs as $song) {
            
            // Create child node
            $xml_track = $xml->createElement('song');

            // Set atribute to album
            //$xml_album->setAttributeNode(new DOMAttr('id', $id));

            // Create name inside the track
            $xml_name = $xml->createElement('name');
            $xml_name->nodeValue = $song['name'];
            $xml_track->appendChild($xml_name);

            $xml_name1 = $xml->createElement('name1');
            $xml_name1->nodeValue = $song['name1'];
            $xml_track->appendChild($xml_name1);

            $short_cont = $xml->createElement('short_cont');
            $short_cont->nodeValue = $song['short_cont'];
            $xml_track->appendChild($short_cont);

            $short_cont1 = $xml->createElement('short_cont1');
            $short_cont1->nodeValue = $song['short_cont1'];
            $xml_track->appendChild($short_cont1);

            $author = $xml->createElement('author');
            $author->nodeValue = $song['author'];
            $xml_track->appendChild($author);

            $code = $xml->createElement('code');
            $code->nodeValue = $song['code'];
            $xml_track->appendChild($code);

            $vol = $xml->createElement('vol');
            $vol->nodeValue = $song['vol'];
            $xml_track->appendChild($vol);
            
            $start_with = $xml->createElement('startwith');
            $start_with->nodeValue = strtolower(substr($song['name1'], 0, 1));
            $xml_track->appendChild($start_with);

            // Push track into album
            $xml_album->appendChild($xml_track);

        }
        
        // Push album into xml
        $xml->appendChild($xml_album);
        
        $xml->save(dirname(__FILE__). '/../../public/data/songs.xml');
        exit;
    } 
    
    public function filter_vn_name_places() {
        $data = $this->Places_model->get_all()->result_array();
        foreach ($data as $item) {
            $nameid = str_replace('   ', ' ', $this->vn_str_filter($item['name']));
            $nameid = str_replace('  ', ' ', $nameid);
            $nameid = str_replace(' ', '-', $nameid);
            $last = substr($nameid, strlen($nameid) - 1, 1);
            if ($last == '-') {
                $nameid = substr($nameid, 0, strlen($nameid) - 1);
            }
            $this->Places_model->update(array('nameid' => $nameid), $item['id']);
        }
        exit;
    }
}